/******************************************************************************
 * @File name   :      RedemptionController.java
 *
 * @Author      :      JIZHANGS
 *
 * @Date        :      Jul 30, 2015
 *
 * @Copyright Notice: 
 * Copyright (c) 2015 Capgemini, Inc. All  Rights Reserved.
 * This software is published under the terms of the Capgemini Software
 * License version 1.0, a copy of which has been included with this
 * distribution in the LICENSE.txt file.
 * 
 * 
 * ----------------------------------------------------------------------------
 * Date                   Who         Version        Comments
 * Jul 30, 2015 9:24:54 AM        JIZHANGS     1.0            Initial Version
 *****************************************************************************/
package com.cap.bts.crmdp.dealer.redemption.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cap.bts.crmdp.dealer.purchase.domain.MemberPackageInfo;
import com.cap.bts.crmdp.dealer.purchase.service.IMemberPackageService;
import com.cap.bts.crmdp.dealer.redemption.dto.MemberPackageInfoResultsDto;
import com.cap.bts.crmdp.dealer.redemption.dto.MemberQueryDto;
import com.cap.bts.crmdp.dealer.redemption.dto.RedemptionSubmitDto;
import com.cap.bts.crmdp.dealer.redemption.service.IRedemptionService;
import com.cap.bts.framework.common.controller.BaseController;
import com.cap.bts.framework.common.exception.AppException;

/**
 *会员流程-使用
 */
@Controller
@RequestMapping(value = "/crmdp/dealer/redemption")
public class RedemptionController extends BaseController {
        
    static final String GETMAINTAINMEMBERPAGE = "getMaintainMemberPage";
    @Autowired
    IRedemptionService redemptionService;
    @Autowired
    IMemberPackageService  memberPackageService;
    
    /**
     * 转向选择套餐
     * 
     * @Date : 2015-7-22
     * @param model
     *            Model
     * @param request
     *            HttpServletRequest
     * @return String
     * @throws AppException
     */
    @RequestMapping(value = "/showMemberPackageList")
    public String showMemberPackageList(MemberQueryDto dto,Model model, HttpServletRequest request) throws AppException {
        List<MemberPackageInfoResultsDto> list=redemptionService.getPackageListByMember(dto);
        model.addAttribute("memberPackageInfoList", list);
        model.addAttribute("memberInfo", dto);
        com.cap.bts.crmdp.dealer.member.dto.MemberQueryDto memberQueryDto = 
            (com.cap.bts.crmdp.dealer.member.dto.MemberQueryDto) request.getSession().getAttribute("getMaintainMemberPage");
        if(memberQueryDto != null && dto != null){
            memberQueryDto.setIsSelected("radio"+ dto.getId() +dto.getCarPlate() +"Rad");
        }
        return "/redemption/showMemberPackageList";
    }
    
  
    /**
     * 检查该会员是否存在激活后的套餐
     * 
     * @Date : 2015-7-22
     * @param model
     *            Model
     * @param request
     *            HttpServletRequest
     * @return String
     * @throws AppException
     */
    @ResponseBody
    @RequestMapping(value = "/checkActivePackageExsit")
    public Boolean checkActivePackageExsit(MemberQueryDto dto, Model model, HttpServletRequest request) throws AppException {
            return redemptionService.checkActivePackage(dto);
    }
    @ResponseBody
    @RequestMapping(value = "/checkPackageExsit")
    public Boolean checkPackageExsit(MemberQueryDto dto, Model model, HttpServletRequest request) throws AppException {
        return redemptionService.checkPackage(dto);
    }
    
    /**
     * 
     * @param dto
     * @param model
     * @param request
     * @return
     * @throws AppException
     */
    @ResponseBody
    @RequestMapping(value = "/sendRedemptionCode")
    public boolean sendRedemptionCode(Long redemptionId, Long memberPackageId) throws AppException {
         return redemptionService.sendRedemptionCode(redemptionId,memberPackageId);
    }
    
    
    /**
     * 检查该会员是否存在激活后的套餐
     * 
     * @Date : 2015-7-22
     * @param model
     *            Model
     * @param request
     *            HttpServletRequest
     * @return String
     * @throws AppException
     */
    @ResponseBody
    @RequestMapping(value = "/checkActivicationCodeExsit")
    public Boolean checkActivicationCodeExsit(RedemptionSubmitDto dto, Model model, HttpServletRequest request) throws AppException {
            if(redemptionService.checkActivicationCode(dto)!=null){
                if(dto.getIsLast()){
                    MemberPackageInfo mpi= new MemberPackageInfo();
                    mpi.setId(dto.getId());
                    memberPackageService.updateMemberPackageInfoById(mpi);
                }
                return true;
            }
            return false;
    }
    
    @ResponseBody
    @RequestMapping(value = "/useActivicationCode")
    public Boolean useActivicationCode(RedemptionSubmitDto dto, Model model, HttpServletRequest request) throws AppException {
        redemptionService.useActivicationCode(dto);
        if(dto.getIsLast()){
            MemberPackageInfo mpi= new MemberPackageInfo();
            mpi.setId(dto.getId());
            memberPackageService.updateMemberPackageInfoById(mpi);
        }
        return true;
    }
}
